<?php

namespace app\admin\model;

use think\Model;


class ProjectCentralcontact extends Model
{

    // 表名
    protected $name = 'project_centralcontact';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'create_time';
    protected $updateTime = 'update_time';
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'create_time_text',
        'update_time_text',
    ];

    protected static function init()
    {
        ProjectCentralcontact::event('before_insert',function ($data){
            $data['status'] = -1;
            $data -> updateRelaByAdmin($data);
        });

        ProjectCentralcontact::event('before_update',function ($data){
            $data -> updateRelaByAdmin($data);
        });
    }

    protected function updateRelaByAdmin($data){
        $leadersKey = 'leader_ids' ;
        $leadersNamesKey = 'leader_names';
        if(isset($data[$leadersKey]) && $data[$leadersKey]){
            $data[$leadersKey] = (new \app\admin\model\ContactLeaders())->whereIn('id', $data[$leadersKey])->value('GROUP_CONCAT(id)');
            $data[$leadersNamesKey] = (new \app\admin\model\ContactLeaders())->whereIn('id', $data[$leadersKey])->value('GROUP_CONCAT(name)');
        }
        if(isset($data['receive_time']))
        {
            $data['receive_time_desc'] = $data['receive_time'];
            $data['receive_time'] = strtotime($data['receive_time']);
        }
        if(isset($data['contact_typeid']))
        {
            $data['contact_typename'] = config('app.contact_typeid')[$data['contact_typeid']] ?? '';
        }

        $assistKey = 'join_depart_ids' ;
        $assistNamesKey = 'join_depart_names';
        if(isset($data[$assistKey]) && $data[$assistKey]){
            $data[$assistKey] = (new \app\admin\model\Chargedep())->whereIn('id', $data[$assistKey])->value('GROUP_CONCAT(id)');
            $data[$assistNamesKey] = (new \app\admin\model\Chargedep())->whereIn('id', $data[$assistKey])->value('GROUP_CONCAT(name)');
        }

        $depKey = 'chargedep_ids' ;
        $depNamesKey = 'chargedep_names';
        if(isset($data[$depKey]) && $data[$depKey]){
            $data[$depKey] = (new \app\admin\model\Chargedep())->whereIn('id', $data[$depKey])->value('GROUP_CONCAT(id)');
            $data[$depNamesKey] = (new \app\admin\model\Chargedep())->whereIn('id', $data[$depKey])->value('GROUP_CONCAT(name)');
        }


        $officeKey = 'centraloffice_id';
        $officeNamesKey = 'centraloffice_name';
        if(isset($data[$officeKey])){
            $data[$officeKey] = (new \app\admin\model\Centraloffice())->whereIn('id', $data[$officeKey])->value('GROUP_CONCAT(id)');
            $data[$officeNamesKey] = (new \app\admin\model\Centraloffice())->whereIn('id', $data[$officeKey])->value('GROUP_CONCAT(name)');
        }



        
    }

    public function getCreateTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['create_time']) ? $data['create_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getUpdateTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['update_time']) ? $data['update_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    protected function setCreateTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setUpdateTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }


}
